import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"
import { store } from "../store"
import nprogress from "nprogress"
import "nprogress/nprogress.css"
import home from "@cop/index/blogs.vue"
import blogDetail from "@cop/blogDetail/blogDetail.vue"
import essays from "@cop/essays/index.vue"
import about from "@cop/about/index.vue"

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    name: "edit",
    path: "/edit",
    component: () => import("@cop/editor/index.vue"),
    meta: {
      title: "编辑",
    },
  },
  {
    name: "home",
    path: "/home",
    component: home,
    meta: {
      title: "主页",
      opacity: "0.9",
    },
  },
  {
    name: "blogDetail",
    path: "/blogDetail/:id",
    component: blogDetail,
    meta: {
      title: "博文详情",
      opacity: "0.9",
    },
  },
  {
    name: "essays",
    path: "/essays",
    component: essays,
    meta: {
      title: "随笔",
      opacity: "0.6",
    },
  },
  {
    name: "about",
    path: "/about",
    component: about,
    meta: {
      title: "关于",
      opacity: "0.6",
    },
  },
  {
    path: "/:pathMatch(.*)*",
    redirect: "/",
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

//nprogress 配置
nprogress.configure({
  easing: "ease",
  speed: 500,
  trickleSpeed: 200,
  showSpinner: false,
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  store.commit("setRootOpacity", to.meta.opacity)
  nprogress.start()
  next()
})
router.afterEach(() => {
  nprogress.done()
})

export default router
